package com.ifreshwing.https;

import android.content.Context;
import android.os.AsyncTask;

import java.util.HashMap;
import java.util.Map;

/**
 * 网络请求任务
 * 
 * @author lz
 * 
 */
public class NetWorkTask extends AsyncTask<Object, Integer, Object> {

	@SuppressWarnings("unused")
	private Context context;
	private INetWorkCallBack iNetWorkCallBack;
	private int requestId;
	private NetWorkConnector netWorkConnector;
	private Map<String, String> map;
	private int type;

	public NetWorkTask(INetWorkCallBack iNetWorkCallBack, int requestId) {
		this.context = (Context) iNetWorkCallBack;
		this.requestId = requestId;
		this.iNetWorkCallBack = iNetWorkCallBack;
		netWorkConnector = NetWorkConnector.getNetWorkConnector();
	}

	public NetWorkTask(INetWorkCallBack iNetWorkCallBack, Context context) {
		this.context = context;
		this.iNetWorkCallBack = iNetWorkCallBack;
		netWorkConnector = NetWorkConnector.getNetWorkConnector(context);
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
	}

	@SuppressWarnings("unchecked")
	@Override
	protected Object doInBackground(Object... params) {
		requestId = (Integer) params[0];
		String url;
		switch (requestId) {
		default:
			map = (HashMap<String, String>) params[1];
			type = (Integer) params[2];
			url = UrlStrings.getUrl(requestId);
			break;
		}
		return netWorkConnector.sendRequest(requestId, type, url, map);

	}

	@Override
	protected void onPostExecute(Object result) {
		super.onPostExecute(result);
		iNetWorkCallBack.onNetWorkResponse(requestId, result);
	}
	
	

}
